package org.tourism.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.tourism.VO.ComplaintHandlingVO;
import org.tourism.VO.ComplaintVO;
import org.tourism.VO.UserVO;
import org.tourism.aop.CheckLogin;
import org.tourism.common.CommonResponse;
import org.tourism.service.ComplaintHandlingService;
import org.tourism.service.ComplaintService;
import org.tourism.service.UserService;
import org.tourism.service.impl.OssService;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/complaint")
public class ComplaintController {

    @Autowired
    private ComplaintService complaintService;

    @Autowired
    private ComplaintHandlingService complaintHandlingService;

    @Autowired
    private UserService userService;

    @Autowired
    private OssService ossService;

    //游客投诉
    @PostMapping
    @CheckLogin
    public CommonResponse<ComplaintVO> complaint(@RequestParam("content") String content,
                                                 @RequestParam("uid") int uid,
                                                 @RequestParam("time") String time,
                                                 @RequestPart("attachment") MultipartFile attachment) throws IOException {
        ComplaintVO complaintVO = new ComplaintVO();
        complaintVO.setContent(content);
        String savePath = "complaint/" + uid + "_" + time + ".jpg";
        if(!attachment.isEmpty()){
            String url = ossService.uploadImage(attachment, savePath);
            complaintVO.setAttachment(url);
        }
        UserVO userVO = userService.getUserById(uid);
        complaintVO.setUserVO(userVO);
        //之后写了DateUtil之后来改这里
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH:mm:ss");
        complaintVO.setDateTime(LocalDateTime.parse(time, formatter));
        complaintService.addComplaint(complaintVO);
        return CommonResponse.createForSuccess("成功添加投诉", complaintVO);
    }

    //投诉审核，0未审核，1审核通过，2审核不通过
    @PostMapping("/review/{complaintId}")
    @CheckLogin
    public CommonResponse<ComplaintVO> complaintReview(@PathVariable int complaintId,
                                          @RequestParam int reviewStatus){
        if(complaintService.getProcessStatusById(complaintId) != 0){
            return CommonResponse.createForError(1, "重复审核同一条投诉");
        }
        complaintService.updateReviewStatusById(complaintId, reviewStatus);
        ComplaintVO complaintVO = new ComplaintVO();
        complaintVO.setId(complaintId);
        complaintVO.setReviewStatus(reviewStatus);
        return CommonResponse.createForSuccess("完成投诉审核", complaintVO);
    }

    //获取所有未审核的投诉给审核人员
    @GetMapping("/unreviewed")
    @CheckLogin
    public CommonResponse<List<ComplaintVO>> getUnreviewedComplaint(){
        return CommonResponse.createForSuccess("成功获取未审核的投诉", complaintService.getUnreviewedComplaints());
    }

    //获取某个游客的所有投诉
    @GetMapping("/user/{uid}")
    @CheckLogin
    public CommonResponse<List<ComplaintVO>> getComplaintForUser(@PathVariable int uid){
        return CommonResponse.createForSuccess("成功获取该游客的投诉", complaintService.getComplaintsByUid(uid));
    }

    //删除一条投诉
    @DeleteMapping("/{complaintId}")
    @CheckLogin
    public CommonResponse deleteComplaint(@PathVariable int complaintId){
        if(complaintService.deleteComplaintById(complaintId) == 1){
            return CommonResponse.createForSuccess("删除投诉成功");
        }else{
            return CommonResponse.createForError("删除投诉失败");
        }
    }

    @GetMapping("/unprocessed")
    @CheckLogin
    public CommonResponse<List<ComplaintVO>> getUnresolvedComplaint(){
        return CommonResponse.createForSuccess("成功获取未处理的投诉", complaintService.getUnresolvedComplaints());
    }

    //处理投诉
    @PostMapping("/process/{complaintId}")
    @CheckLogin
    public CommonResponse<ComplaintHandlingVO> complaintProcess(@PathVariable int complaintId,
                                                                @RequestParam String content,
                                                                @RequestParam String time,
                                                                @RequestPart MultipartFile attachment) throws IOException {
        ComplaintHandlingVO complaintHandlingVO = new ComplaintHandlingVO();
        String savePath = "complaint/" + complaintId + "_" + time + ".jpg";
        if(!attachment.isEmpty()){
            String url = ossService.uploadImage(attachment, savePath);
            complaintHandlingVO.setAttachment(url);
        }
        complaintHandlingVO.setComplaintId(complaintId);
        complaintHandlingVO.setContent(content);
        //之后写了DateUtil之后来改这里
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH:mm:ss");
        complaintHandlingVO.setTime(LocalDateTime.parse(time, formatter));
        complaintHandlingService.addComplaintHandling(complaintHandlingVO);
        complaintService.updateProcessStatusById(complaintId);
        return CommonResponse.createForSuccess("成功处理一条投诉", complaintHandlingVO);
    }
}
